//
//  NYEssenceViewController.m
//  百思不得姐
//
//  Created by Apple on 15/8/22.
//  Copyright (c) 2015年 Neal. All rights reserved.
//

#import "NYEssenceViewController.h"
#import "NYTagViewController.h"
#import "NYTitleViewButton.h"

#import "NYAllViewController.h"
#import "NYVideoViewController.h"
#import "NYVoiceViewController.h"
#import "NYPictureViewController.h"
#import "NYWordViewController.h"

@interface NYEssenceViewController ()<UIScrollViewDelegate>
/* 所有tableview的父控件 */
@property(nonatomic , weak) UIScrollView *scrollView;
/* 所有标题的父控件 */
@property(nonatomic , weak) UIView *titleView;
/* titleView的下划线 */
@property(nonatomic , weak) UIView *titleUnderLineView;
/* selectedbutton,记录被选中的按钮 */
@property(nonatomic , weak) UIButton *selectedTitleButton;
/* 所有title按钮 */
@property(nonatomic , strong) NSMutableArray *titleButtons;

@end

@implementation NYEssenceViewController
#pragma mark - 懒加载
- (NSMutableArray *)titleButtons
{
    if (_titleButtons == nil) {
        _titleButtons = [NSMutableArray array];
    }
    return _titleButtons;
}
#pragma mark - 初始化
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = NYGlobalBg;
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self setupNav];
    [self setupScrollView];
    [self setupTitleView];
    [self setupchildVC];
    /**
     *  默认选中0个View
     */
    [self scrollViewDidEndScrollingAnimation:self.scrollView];
    
}
- (void)setupNav
{
    self.navigationItem.titleView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"MainTitle"]];
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithTarget:self action:@selector(btnClick) image:[UIImage imageNamed:@"MainTagSubIcon"] Highlightedimage:[UIImage imageNamed:@"MainTagSubIconClick"]];
    
}
- (void)setupScrollView
{
    UIScrollView *scrollView = [[UIScrollView alloc]init];
    scrollView.frame = self.view.bounds;
    scrollView.delegate = self;
    scrollView.pagingEnabled = YES;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:scrollView];
    self.scrollView = scrollView;
}

- (void)btnClick{
    NYTagViewController *tagvc = [[NYTagViewController alloc]init];
    [self.navigationController pushViewController:tagvc animated:YES];
}

//添加子控制器，通过addChildViewController实现懒加载子控制器
- (void)setupchildVC
{
    NYAllViewController *allvc = [[NYAllViewController alloc]init];
    allvc.title = @"全部";
    [self addChildViewController:allvc];
    
    NYVideoViewController *video = [[NYVideoViewController alloc]init];
    video.title = @"视频";
    [self addChildViewController:video];
    
    NYVoiceViewController *voice = [[NYVoiceViewController alloc]init];
    voice.title = @"声音";
    [self addChildViewController:voice];
    
    NYPictureViewController *picture = [[NYPictureViewController alloc]init];
    picture.title = @"图片";
    [self addChildViewController:picture];
    
    NYWordViewController *word = [[NYWordViewController alloc]init];
    word.title = @"文字";
    [self addChildViewController:word];

    CGFloat contentW = self.childViewControllers.count * self.scrollView.width;
    
    self.scrollView.contentSize = CGSizeMake(contentW, 0);
}

#pragma mark - <UIScrollViewDelegate>
//监听scrollview滚动动画完成时，（非人为拖拽）
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    NSInteger index = self.scrollView.contentOffset.x / scrollView.width;
    UIViewController *willshowVC = self.childViewControllers[index];
    if (willshowVC.isViewLoaded) {
        return;
    }
    willshowVC.view.frame = self.scrollView.bounds;
    [scrollView addSubview:willshowVC.view];
}

//人为拖拽时，监听scrollview的滚动
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger index = self.scrollView.contentOffset.x / scrollView.width;
    
    [self titleButtonClick:self.titleButtons[index]];
    
    [self scrollViewDidEndScrollingAnimation:scrollView];
}

#pragma mark - 设置标题试图
- (void)setupTitleView
{
    /**
     头部
     */
    UIView *titleView = [[UIView alloc]init];
    [self.view addSubview:titleView];
    self.titleView = titleView;
    titleView.width = self.view.width;
    titleView.height = titleViewHeight;
    titleView.y = NYnavigationBarMaxY;
    titleView.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.5];
    /**
     底部线
     */
    UIView *titleUnderLineView = [[UIView alloc]init];
    [titleView addSubview:titleUnderLineView];
    self.titleUnderLineView = titleUnderLineView;

    titleUnderLineView.height = 2;
    titleUnderLineView.y = titleView.height - titleUnderLineView.height;
    titleUnderLineView.backgroundColor = [UIColor redColor];

    
    //添加标签按钮
    NSArray *titleTags = @[@"全部",@"视频",@"声音",@"图片",@"段子"];
    
    CGFloat buttonW = titleView.width / titleTags.count;
    CGFloat buttonH = titleView.height;
    for (int i = 0; i<titleTags.count; i++) {
        NYTitleViewButton *button = [NYTitleViewButton buttonWithType:UIButtonTypeCustom];
        button.tag = i;
        [button setTitle:titleTags[i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
        [button addTarget:self action:@selector(titleButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [titleView addSubview:button];
        [self.titleButtons addObject:button];
        
        button.width = buttonW;
        button.height = buttonH;
        button.x = i * buttonW;
        button.y = 0;
        
        if (i == 0) {
            button.selected = YES;
            _selectedTitleButton = button;
            [button.titleLabel sizeToFit];
            titleUnderLineView.width = button.titleLabel.width;
            titleUnderLineView.centerX = button.centerX;
        }
    }
    
}
#pragma mark - 监听按钮点击
- (void)titleButtonClick:(UIButton *)button
{
    self.selectedTitleButton.selected = NO;
    button.selected = YES;
    _selectedTitleButton = button;
    
    [UIView animateWithDuration:0.25 animations:^{
        self.titleUnderLineView.centerX = button.centerX;
        self.titleUnderLineView.width = button.titleLabel.width;

    }];
    /**
     *  偏移量
     */
    CGPoint offset = self.scrollView.contentOffset;
    offset.x = button.tag * self.scrollView.width;
    [self.scrollView setContentOffset:offset animated:YES];
}


@end
